package cn.lili.modules.order.order.entity.dto;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.UserEnums;
import cn.lili.common.utils.DateUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.order.cart.entity.enums.DeliveryMethodEnum;
import cn.lili.modules.order.order.entity.enums.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

@EqualsAndHashCode(callSuper = true)
@Data
public class TradeSearchParams extends PageVO {

    private static final long serialVersionUID = -6380527333908995994L;

    @ApiModelProperty(value = "交易编号")
    private String sn;

    @ApiModelProperty(value = "页面标签",
            example = "ALL:全部," +
                    "WAIT_PAY:待付款," +
                    "WAIT_DELIVERED:待配货," +
                    "WAIT_CALLED:待装车," +
                    "CANCELLED:已取消," +
                    "COMPLETE:已完成")
    private String tag;

    @ApiModelProperty(value = "主商家ID")
    private String storeId;

    @ApiModelProperty(value = "会员ID")
    private String memberId;

    @ApiModelProperty(value = "期望配送时间")
    private Date deliveryTime;

    @ApiModelProperty(value = "交易订单状态")
    private String tradeStatus;

    /**
     * @see DeliveryMethodEnum
     */
    @ApiModelProperty(value = "配送方式")
    private String deliveryMethod;

    public <T> QueryWrapper<T> queryWrapper() {
        AuthUser currentUser = UserContext.getCurrentUser();
        QueryWrapper<T> wrapper = new QueryWrapper<>();

        if (currentUser != null) {
            //按卖家查询
            wrapper.eq(CharSequenceUtil.equals(currentUser.getRole().name(), UserEnums.STORE.name()), "t.store_id", currentUser.getStoreId());

            //店铺查询
            wrapper.eq(CharSequenceUtil.equals(currentUser.getRole().name(), UserEnums.MANAGER.name())
                    && CharSequenceUtil.isNotEmpty(storeId), "t.store_id", storeId);

            //按买家查询
            wrapper.eq(CharSequenceUtil.equals(currentUser.getRole().name(), UserEnums.MEMBER.name()) && memberId == null, "t.member_id", currentUser.getId());

        }
        //按照买家查询
        wrapper.like(CharSequenceUtil.isNotEmpty(memberId), "t.member_id", memberId);

        //按交易编号查询
        wrapper.like(CharSequenceUtil.isNotEmpty(sn), "t.sn", sn);

        //按时间查询
        wrapper.ge(deliveryTime != null, "t.delivery_time", deliveryTime);

        //按配送类型查询
        wrapper.eq(CharSequenceUtil.isNotEmpty(deliveryMethod), "t.delivery_method", deliveryMethod);

        //订单状态
        wrapper.eq(CharSequenceUtil.isNotEmpty(tradeStatus), "t.trade_status", tradeStatus);

        //按标签查询
        if (CharSequenceUtil.isNotEmpty(tag)) {
            String orderStatusColumn = "t.trade_status";
            OrderTagEnum tagEnum = OrderTagEnum.valueOf(tag);
            switch (tagEnum) {
                //待付款
                case WAIT_PAY:
                    wrapper.eq(orderStatusColumn, OrderStatusEnum.UNPAID.name());
                    break;
                //备货中 （已支付  待配货  已配货）
                case WAIT_DELIVERED:
                    wrapper.and(statusWrapper -> statusWrapper.eq(orderStatusColumn, OrderStatusEnum.PAID.name()).or().eq(orderStatusColumn, OrderStatusEnum.UNDELIVERED.name()).or().eq(orderStatusColumn,OrderStatusEnum.DELIVERED.name()) );
                    break;
                //装车中   （待叫车 已叫车 待装车  已装车）
                case WAIT_CALLED:
                    wrapper.and(statusWrapper -> statusWrapper.eq(orderStatusColumn, OrderStatusEnum.UNCALLED.name()).or().eq(orderStatusColumn, OrderStatusEnum.CALLED.name()).or().eq(orderStatusColumn,OrderStatusEnum.UNENTRUCKED.name()).or().eq(orderStatusColumn,OrderStatusEnum.ENTRUCKED.name()) );
                    break;
                //已取消
                case CANCELLED:
                    wrapper.eq(orderStatusColumn, OrderStatusEnum.CANCELLED.name());
                    break;
                //已完成
                case COMPLETE:
                    wrapper.eq(orderStatusColumn, OrderStatusEnum.COMPLETED.name());
                    break;
                default:
                    break;
            }
        }
        wrapper.eq("t.delete_flag", false);
        return wrapper;
    }

}

